package com.th.supcom.pds.drugsupply.client.widget;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.drugsupply.client.model.PdsPackageInfoModel;

public final class DrugSplitUtils
{
    private DrugSplitUtils ()
    {

    }

    public static void setUnedite (final GridEvent <PdsPackageInfoModel> be, final PdsPackageInfoModel pdsPackageInfoModel, final int cloumIndex)
    {
        if (cloumIndex == be.getColIndex () && pdsPackageInfoModel.getFlag ().equals ("0"))
        {
            be.setCancelled (true);
        }
    }

    /**
     * 对明细进行分组
     * 
     * @return add by Zli
     */
    public static GroupingView createGroupingView ()
    {
        final GroupingView view = new GroupingView ();
        view.setEmptyText ("暂时没有您需要的数据...");
        view.setGroupRenderer (new GridGroupRenderer ()
        {
            @Override
            public String render (final GroupColumnData data)
            {
                final String prescNo = data.models.get (0).get ("phamName");
                final String spec = data.models.get (0).get ("phamSpec");
                if (prescNo == null)
                {
                    return "";
                }
                return "药品名称: " + prescNo + "&nbsp&nbsp药品规格:" + spec + " (" + (data.models.size () - 1) + "条明细)";
            }
        });
        return view;
    }

    /**
     * 内存过滤器
     * 
     * @return add by Zli
     */
    public static StoreFilterField <PdsPackageInfoModel> createFilter ()
    {
        StoreFilterField <PdsPackageInfoModel> filter = new StoreFilterField <PdsPackageInfoModel> ()
        {

            @Override
            protected boolean doSelect (final Store <PdsPackageInfoModel> store, final PdsPackageInfoModel parent, final PdsPackageInfoModel record, final String property,
                                        final String filter)
            {
                return filterCondition (record, filter);
            }
        };
        filter.setWidth (350);
        filter.setEmptyText ("输入药品中文名称或者拼音");
        return filter;
    }

    /***
     * 药品查询过滤条件
     * 
     * @param record
     * @param filter
     * @return add by Zli
     */
    private static boolean filterCondition (final PdsPackageInfoModel record, final String filter)
    {
        if ((record.getVphamName () != null && record.getVphamName ().contains (filter)) || ((record.getInputCode () != null && record.getInputCode ().contains (filter))))

        {
            return true;
        }
        return false;
    }

    public static GridCellRenderer <PdsPackageInfoModel> generatorRenderForPackage ()
    {
        return new GridCellRenderer <PdsPackageInfoModel> ()
        {

            @Override
            public Object render (final PdsPackageInfoModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsPackageInfoModel> store, final Grid <PdsPackageInfoModel> grid)
            {
                final String modelname = (ClientUtils.avoidNull (model.getPackageFactor () == null ? 1 : model.getPackageFactor ().intValue ()))
                                         + ClientUtils.avoidNull (model.getPhamUnitName ()) + "/" + (ClientUtils.avoidNull (model.getPackageUnitName ()));
                return strModelName (modelname, model);
            }
        };

    }

    static String strModelName (final String modelname, final ModelData model)
    {
        if ("1".equals (model.get ("flag")) || "1".equals (model.get ("enable")))
        {
            return "<div style='background-color:#8DB6CD;width:71px;height:14px;'>" + modelname + "</div>";
        }
        return modelname;
    }

    /**
     * 公司版权信息.
     */
    public static final String copyright = "Copyright 2014, Carefx Corporation";

    /**
     * 提交路径.
     */
    public static final String SCC_BRANCH = "$URL$";

    /**
     * 提交日期.
     */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /**
     * 最终修改人员.
     */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /**
     * 最终版本号.
     */
    public static final String SCC_REVISION = "$Revision$";
}
